<?php
defined('BASEPATH') OR exit('No direct script access allowed');
/**
 * Description: 搜索模型
 *
 * @author      Eden
 * @datetime    2016/11/28 10:20
 * @copyright   Beijing CmsTop Technology Co.,Ltd.
 */

class Search_Model extends ADM_Model
{
    /**
     * 初始化
     */
    public function __construct()
    {
        parent::__construct();
    }

    public function edit($params = array())
    {
        $kId = $params['kId'];
        try {
            $this->db->where('id', $kId);
            unset($params['kId'], $params['type']);
            return $this->db->update('search', $params);
        } catch(PDOException $e) {}
        return false;
    }

    /**
     * 获取列表
     * @param $offset
     * @param $limit
     * @return array|bool
     */
    public function getKeywords($offset, $limit)
    {
        $db = clone($this->db);
        $total = $db->count_all('search');
        if (empty($total)) {
            return false;
        } else {
            $q = $this->db->order_by('times', 'DESC')
                ->limit($limit, $offset)
                ->get('search');
            $res = $q->result_array();
            if ($res) foreach($res as &$item) {
                $item['id'] = intval($item['id']);
                $item['times'] = intval($item['times']);
            }

            return array(
                'total' => $total,
                'data' => $res
            );
        }
    }

    /**
     * 删除
     * @param int $id
     * @return mixed
     */
    public function remove($id = 0)
    {
        try {
            $db = clone($this->db);
            $db->where('id', $id);
            $db->delete('search');
            return $db->affected_rows();
        } catch(PDOException $e) {}
        return false;
    }

    /**
     * 添加关键字
     * @param $params
     * @return array|bool
     */
    public function add($params)
    {
        try {
            $this->db->insert('search', $params);
            $lastId = $this->db->insert_id();
            return array_merge($params, array('id' => $lastId));
        } catch(PDOException $e) {}
        return false;
    }

}
